package org.itheima.controller;
import org.itheima.pojo.Article;
import org.itheima.pojo.PageBean;
import org.itheima.pojo.Result;
import org.itheima.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    //新增文章
    @PostMapping
    public Result addArticle(@RequestBody @Validated(Article.Add.class) Article article) {
        articleService.addArticle(article);
        return Result.success("success");
    }

    //文章列表
    @GetMapping
    public Result<PageBean<Article>> list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer categoryId,//使用RequestParam标注不是必须传入参数可以为空
            @RequestParam(required = false)String state
    ){
       PageBean<Article> pb =  articleService.list(pageNum,pageSize,categoryId,state);
       return Result.success(pb);
    }

    //获取文章详情页
    @GetMapping("/detail")
    public Result detail(Integer id) {
        Article article = articleService.findByid(id);
        return Result.success(article);
    }

    //更新文章
    @PutMapping
    public Result update(@RequestBody @Validated(Article.Update.class) Article article) {
        articleService.update(article);
        return Result.success("更新文章success");
    }
    //删除文章
    @DeleteMapping
    public Result delete(Integer id) {
        articleService.del(id);
        return Result.success("删除文章success");
    }

}

